﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using TFSUtils.Helpers;
using TFSUtils.Model;

namespace TFSUtils.ViewModel
{
    public class BuildAnalysisViewModel : ViewModelBase
    {
        public RelayCommand GenerateResultsCommand { get; private set; }

        private BuildAnalysis _buildAnalysis;
        public BuildAnalysis BuildAnalysis
        {
            get
            {
                if (_buildAnalysis == null)
                    _buildAnalysis = new BuildAnalysis();

                return _buildAnalysis;
            }
            /*
            set
            {
                _buildAnalysis = value;
                RaisePropertyChanged();
            }
            */
        }

        public BuildAnalysisViewModel()
        {
            //_tfsUri = tfsURI;
            GenerateResultsCommand = new RelayCommand(GenerateResults);

            BuildAnalysis.TFSBasicData = SimpleIoc.Default.GetInstance<TFSBasicData>();

            // Put in data service
            var buildData = SerialiseHelper.DeserialiseObject<BuildAnalysis>() as BuildAnalysis;
            if (buildData != null)
            {                
                this.BuildAnalysis.BuildName = buildData.BuildName;
                this.BuildAnalysis.Project = buildData.Project;
                this.BuildAnalysis.SourceBranch = buildData.SourceBranch;
                this.BuildAnalysis.TargetBranch = buildData.TargetBranch;                
            }
        }

        private ObservableCollection<StatusItem> _results;
        public  ObservableCollection<StatusItem> Results
        {
            get
            {
                return _results;
            }
            set
            {
                _results = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// Generate the build analysis
        /// </summary>
        private void GenerateResults()
        {
            TFSBasicData data = SimpleIoc.Default.GetInstance<TFSBasicData>();
            var results = TFSUtilLibrary.Builds.GetAssociatedWorkItems(
                data.URI, 
                BuildAnalysis.Project, 
                BuildAnalysis.BuildName,
                BuildAnalysis.SourceBranch,
                BuildAnalysis.TargetBranch);

            // Save the data is the screen
            SerialiseHelper.SerialiseObject(BuildAnalysis);

            if (results == null)
            {
                Results = new ObservableCollection<StatusItem>();

            }
            else
            {
                Results = new ObservableCollection<StatusItem>(results.Select(x => new StatusItem()
                {
                    Id = x.WorkItemNumber,
                    Description = x.WorkItemDescription,
                    ItemType = StatusType.Information
                }));
            }
        }

    }
}
